/*
 * Copyright (C) 2013～2023 上海颐凡软件科技有限公司
 * Yfann Software Technology (Shanghai) Co.,LTD
 * All Rights Reserved.
 * 公司网址: www.yfann.com
 * 365IT教育网，成就您优秀架构师之梦！
 * 平台网址: www.365itedu.com
 */
package com.yfann.web.base.sys.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yfann.sword.web.fwk.bean.AuthBean;
import com.yfann.sword.web.fwk.bean.UIOBean;
import com.yfann.sword.web.fwk.checker.RegEXChecker;
import com.yfann.sword.web.fwk.consts.FrameworkConst;
import com.yfann.sword.web.fwk.filter.AuthorizationController;
import com.yfann.sword.web.fwk.util.RequestUtil;

/**
 * 用户访问URL权限控制类。
 * 
 * <pre>
 * 修改履历
 * -----------------------------------------------------------------------------
 *         VERSION       DATE            BY       CHANGE/COMMENT
 * -----------------------------------------------------------------------------
 *          1.0         2013/10/11      颜廷吉            create
 * -----------------------------------------------------------------------------
 * </pre>
 */
public class AuthorizationControllerImpl implements AuthorizationController {
    /**
     * 路径容器
     */
    private List<String> listCheckRequiredPath;

    /**
     * 日志
     */
    private Log log = LogFactory.getLog(AuthorizationControllerImpl.class);

    /**
     * 进行访问权限验证。
     * @param pathInfo String
     * @param req ServletRequest
     * @return 有访问权限的情况返回true、没有权限的情况返回false。
     */
    public boolean isAuthorized(String pathInfo, ServletRequest req) {
        if (log.isDebugEnabled()) {
            log.debug("isAuthorized() strated.");
        }
        HttpSession session = ((HttpServletRequest) req).getSession();

        UIOBean uio = (UIOBean) session.getAttribute(FrameworkConst.USER_INFO_OBJECT_KEY);
        if (uio != null) {
            List<AuthBean> authBeanList = uio.getAuthBeanList();
            for (AuthBean authBean : authBeanList) {
                String checkedPath = RequestUtil.getActionPathInfo(req);
                String authPathURL =authBean.getAuthURL();
                if (RegEXChecker.validate(authPathURL,checkedPath )) {
                    return true;
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("isAuthorized() ended.");
        }
        return false;
    }

    /**
     * 需要权限验证的情况下返回true；不需要的情况下返回false。
     * @param req ServletRequest
     * @return boolean
     */
    public boolean isCheckRequired(ServletRequest req) {
        if (log.isDebugEnabled()) {
            log.debug("isCheckRequired() strated.");
        }

        for (String pathRegex : listCheckRequiredPath) {
            String reqPath = RequestUtil.getPathInfo(req);
            if (RegEXChecker.validate(pathRegex, reqPath)) {
                return true;
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("isCheckRequired() ended.");
        }
        //
        return false;
    }

    /**
     * @return listCheckRequiredPath
     */
    public List<String> getListCheckRequiredPath() {
        return listCheckRequiredPath;
    }

    /**
     * @param listCheckRequiredPath listCheckRequiredPath
     */
    public void setListCheckRequiredPath(List<String> listCheckRequiredPath) {
        this.listCheckRequiredPath = listCheckRequiredPath;
    }

}
